#ifndef SEU_HTTP_H
#define SEU_HTTP_H
#include"include.h"


// Will poll for responses from an http server MAX_TIMEOUTS times
// with this timeout time in milliseconds
#define RESP_WAIT_TIME 500
#define MAX_TIMEOUTS 8

/*
 * This implements HTTP/1.1 as outlined at:
 * http://www.w3.org/Protocols/rfc2616/rfc2616.html
 */

// NOTE - Full functionality only guarunteed for HTTP/1.1

// HTTP Request
typedef struct {
   // Request Line
   char *method;
   char *reqURI;
   char *protoVersion;

   // Headers
   dict_t *headers; // Dictionary of HTTP headers

   // Message-body
   char *body;
   int  bodylen;

   // Request Parameters
   dict_t *parameters;
} httpreq_t;

#define HTTPREQ_DEFAULT {NULL, NULL, "HTTP/1.1", NULL, NULL, 0, NULL}

// HTTP Response
typedef struct {
   short error;            // Set if there is an error making a request

   // Status Line
   char *protoVersion;
   int  statusCode;

   // Headers
   dict_t *headers;        // All headers thrown into one dict

   // Message-body
   char *body;
   int bodylen;
} httpresp_t;

#define HTTPRESP_DEFAULT {0, NULL, 400, NULL, NULL, 0}

// Turns an httpreq_t into a raw http request
mystr *makeHTTPRequestData(httpreq_t req);

// Makes an http request out of the given httpreq_t, casting the request and
// parsing the http response into an httpresp_t. If an error occurs, the error
// field of the httpresp_t is set to 1.
httpresp_t make_request(httpreq_t req);

mystr *httpGetUrlBody(char *url, dict_t *params);
char *parseUri(char *hostdata);
// Properly frees all memory allocated for an httpresp_t by make_request
void httprespFree(httpresp_t *resp);

#endif
